<template>
  <header class="app-header">
    <nav class="nav-container">
      <div class="nav-brand">
        <router-link to="/" class="brand-link">
          <div class="brand-avatar" :class="{ admin: authStore.isAdmin }">
            <img v-if="authStore.isAuthenticated && authStore.userAvatar && authStore.userAvatar !== '/default-avatar.png'" 
                 :src="authStore.userAvatar" 
                 alt="用户头像" 
                 class="avatar-img" />
            <Crown v-else-if="authStore.isAdmin" :size="20" />
            <Users v-else :size="20" />
          </div>
          <span class="brand-text">AI故事坊</span>
        </router-link>
      </div>

      <div class="nav-menu" :class="{ 'nav-menu-open': isMenuOpen }">
        <router-link 
          v-for="item in navigationItems" 
          :key="item.name"
          :to="item.path" 
          class="nav-link"
          @click="closeMenu"
        >
          <component :is="item.icon" :size="18" />
          <span>{{ item.label }}</span>
        </router-link>
      </div>

      <div class="nav-actions">
        <div v-if="authStore.isAuthenticated" class="user-menu">
          <div class="user-info" @click="toggleUserMenu">
            <div class="user-avatar" :class="{ admin: isAdmin }">
              <img v-if="authStore.userAvatar && authStore.userAvatar !== '/default-avatar.png'" 
                   :src="authStore.userAvatar" 
                   alt="用户头像" 
                   class="avatar-img" />
              <Crown v-else-if="isAdmin" :size="20" />
              <Users v-else :size="20" />
            </div>
            <span class="user-name">{{ authStore.currentUser?.name }}</span>
            <ChevronDown :size="16" :class="{ 'rotate-180': isUserMenuOpen }" />
          </div>
          <div v-if="isUserMenuOpen" class="user-dropdown">
            <router-link to="/profile" class="dropdown-item" @click="closeUserMenu">
              <User :size="16" />
              <span>个人资料</span>
            </router-link>
            <router-link to="/library" class="dropdown-item" @click="closeUserMenu">
              <BookOpen :size="16" />
              <span>我的故事</span>
            </router-link>
            <div v-if="authStore.isAdmin" class="dropdown-divider"></div>
            <router-link 
              v-if="authStore.isAdmin" 
              to="/admin" 
              class="dropdown-item" 
              @click="closeUserMenu"
            >
              <Settings :size="16" />
              <span>管理后台</span>
            </router-link>
            <div class="dropdown-divider"></div>
            <button class="dropdown-item logout-btn" @click="handleLogout">
              <LogOut :size="16" />
              <span>退出登录</span>
            </button>
          </div>
        </div>
        
        <div v-else class="auth-buttons">
          <button class="btn btn-ghost btn-sm" @click="showLoginModal">
            登录
          </button>
          <button class="btn btn-primary btn-sm" @click="showRegisterModal">
            注册
          </button>
        </div>

        <button class="mobile-menu-toggle" @click="toggleMenu">
          <Menu v-if="!isMenuOpen" :size="24" />
          <X v-else :size="24" />
        </button>
      </div>
    </nav>

    <!-- Mobile Menu -->
    <div v-if="isMenuOpen" class="mobile-menu">
      <div class="mobile-menu-content">
        <router-link to="/" class="mobile-menu-item" @click="closeMenu">
          首页
        </router-link>
        <router-link to="/generate" class="mobile-menu-item" @click="closeMenu">
          AI生成
        </router-link>
        <router-link to="/library" class="mobile-menu-item" @click="closeMenu">
          故事库
        </router-link>
        <router-link to="/ranking" class="mobile-menu-item" @click="closeMenu">
          排行榜
        </router-link>
        
        <div v-if="!authStore.isAuthenticated" class="mobile-auth-buttons">
          <button class="btn btn-ghost btn-sm" @click="showLoginModal">
            登录
          </button>
          <button class="btn btn-primary btn-sm" @click="showRegisterModal">
            注册
          </button>
        </div>
      </div>
    </div>

    <!-- Login Modal -->
    <LoginModal 
      :is-visible="isLoginModalVisible"
      @close="closeLoginModal"
      @switch-to-register="switchToRegister"
    />

    <!-- Register Modal -->
    <RegisterModal 
      :is-visible="isRegisterModalVisible"
      @close="closeRegisterModal"
      @switch-to-login="switchToLogin"
    />
  </header>
</template>

<script setup>
import { ref, computed, onMounted, onUnmounted } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '../stores/auth'
import { useToast } from 'vue-toastification'
import {
  Menu,
  X,
  Home,
  PenTool,
  BookOpen,
  BarChart3,
  User,
  Users,
  Crown,
  ChevronDown,
  LogOut,
  Settings
} from 'lucide-vue-next'
import LoginModal from './LoginModal.vue'
import RegisterModal from './RegisterModal.vue'

const router = useRouter()
const authStore = useAuthStore()
const toast = useToast()

const isMenuOpen = ref(false)
const isUserMenuOpen = ref(false)

const isAdmin = computed(() => {
  return authStore.currentUser?.role === 'admin'
})
const isLoginModalVisible = ref(false)
const isRegisterModalVisible = ref(false)

const navigationItems = computed(() => [
  { name: 'home', path: '/', label: '首页', icon: Home },
  { name: 'generate', path: '/generate', label: '创作故事', icon: PenTool },
  { name: 'library', path: '/library', label: '故事库', icon: BookOpen },
  { name: 'ranking', path: '/ranking', label: '排行榜', icon: BarChart3 }
])

const toggleMenu = () => {
  isMenuOpen.value = !isMenuOpen.value
}

const closeMenu = () => {
  isMenuOpen.value = false
}

const toggleUserMenu = () => {
  isUserMenuOpen.value = !isUserMenuOpen.value
}

const closeUserMenu = () => {
  isUserMenuOpen.value = false
}

const showLoginModal = () => {
  isLoginModalVisible.value = true
  closeMenu()
}

const showRegisterModal = () => {
  isRegisterModalVisible.value = true
  closeMenu()
}

const closeLoginModal = () => {
  isLoginModalVisible.value = false
}

const closeRegisterModal = () => {
  isRegisterModalVisible.value = false
}

const switchToRegister = () => {
  isLoginModalVisible.value = false
  isRegisterModalVisible.value = true
}

const switchToLogin = () => {
  isRegisterModalVisible.value = false
  isLoginModalVisible.value = true
}

const handleLogout = () => {
  authStore.logout()
  closeUserMenu()
  toast.success('已退出登录')
  router.push('/')
}

const handleClickOutside = (event) => {
  if (!event.target.closest('.user-menu')) {
    isUserMenuOpen.value = false
  }
  if (!event.target.closest('.nav-menu') && !event.target.closest('.mobile-menu-toggle')) {
    isMenuOpen.value = false
  }
}

onMounted(() => {
  document.addEventListener('click', handleClickOutside)
})

onUnmounted(() => {
  document.removeEventListener('click', handleClickOutside)
})
</script>

<style scoped>
.app-header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1000;
  background: rgba(26, 26, 46, 0.95);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border-color);
}

.nav-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 var(--spacing-lg);
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 70px;
}

.nav-brand {
  display: flex;
  align-items: center;
}

.brand-link {
  display: flex;
  align-items: center;
  gap: var(--spacing-sm);
  text-decoration: none;
  color: var(--text-primary);
  font-weight: 600;
  font-size: var(--font-size-lg);
  transition: color var(--transition-fast);
}

.brand-link:hover {
  color: var(--primary-color);
}

.brand-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  background: var(--gradient-primary);
  border-radius: var(--radius-lg);
  color: white;
}

.brand-text {
  background: var(--gradient-primary);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.nav-menu {
  display: flex;
  align-items: center;
  gap: var(--spacing-lg);
}

.nav-link {
  display: flex;
  align-items: center;
  gap: var(--spacing-xs);
  padding: var(--spacing-sm) var(--spacing-md);
  color: var(--text-secondary);
  text-decoration: none;
  border-radius: var(--radius-md);
  transition: all var(--transition-fast);
  font-weight: 500;
}

.nav-link:hover,
.nav-link.router-link-active {
  color: var(--primary-color);
  background: rgba(102, 126, 234, 0.1);
}

.nav-actions {
  display: flex;
  align-items: center;
  gap: var(--spacing-md);
}

.user-menu {
  position: relative;
}

.user-button {
  display: flex;
  align-items: center;
  gap: var(--spacing-sm);
  padding: var(--spacing-sm) var(--spacing-md);
  background: transparent;
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  color: var(--text-primary);
  cursor: pointer;
  transition: all var(--transition-fast);
}

.user-button:hover {
  border-color: var(--primary-color);
  background: rgba(102, 126, 234, 0.05);
}

.brand-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--gradient-primary);
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  overflow: hidden;
  border: 2px solid transparent;
}

.brand-avatar.admin {
  border-color: #8b5cf6;
  box-shadow: 0 0 15px rgba(139, 92, 246, 0.4);
}

.brand-avatar .avatar-img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.user-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--bg-tertiary);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-secondary);
  overflow: hidden;
  border: 2px solid transparent;
}

.user-avatar.admin {
  background: linear-gradient(135deg, #ffd700, #ffed4e);
  color: #8b5a00;
  border-color: #ffd700;
  box-shadow: 0 0 10px rgba(255, 215, 0, 0.3);
}

.user-avatar .avatar-img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.user-name {
  font-weight: 500;
  max-width: 120px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.user-dropdown {
  position: absolute;
  top: calc(100% + var(--spacing-sm));
  right: 0;
  min-width: 200px;
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-xl);
  padding: var(--spacing-sm);
  z-index: 1001;
}

.dropdown-item {
  display: flex;
  align-items: center;
  gap: var(--spacing-sm);
  width: 100%;
  padding: var(--spacing-sm) var(--spacing-md);
  background: transparent;
  border: none;
  border-radius: var(--radius-md);
  color: var(--text-secondary);
  text-decoration: none;
  cursor: pointer;
  transition: all var(--transition-fast);
  font-size: var(--font-size-sm);
}

.dropdown-item:hover {
  background: rgba(102, 126, 234, 0.1);
  color: var(--text-primary);
}

.logout-btn:hover {
  background: rgba(255, 107, 107, 0.1);
  color: var(--error-color);
}

.dropdown-divider {
  height: 1px;
  background: var(--border-color);
  margin: var(--spacing-sm) 0;
}

.auth-buttons {
  display: flex;
  align-items: center;
  gap: var(--spacing-sm);
}

.mobile-menu-toggle {
  display: none;
  background: transparent;
  border: none;
  color: var(--text-primary);
  cursor: pointer;
  padding: var(--spacing-sm);
  border-radius: var(--radius-md);
  transition: background var(--transition-fast);
}

.mobile-menu-toggle:hover {
  background: rgba(255, 255, 255, 0.05);
}

/* Mobile Styles */
@media (max-width: 768px) {
  .nav-container {
    padding: 0 var(--spacing-md);
  }

  .nav-menu {
    position: fixed;
    top: 70px;
    left: 0;
    right: 0;
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border-color);
    flex-direction: column;
    padding: var(--spacing-lg);
    gap: var(--spacing-md);
    transform: translateY(-100%);
    opacity: 0;
    visibility: hidden;
    transition: all var(--transition-normal);
  }

  .nav-menu-open {
    transform: translateY(0);
    opacity: 1;
    visibility: visible;
  }

  .nav-link {
    width: 100%;
    justify-content: flex-start;
    padding: var(--spacing-md);
    font-size: var(--font-size-base);
  }

  .mobile-menu-toggle {
    display: block;
  }

  .user-name {
    display: none;
  }

  .auth-buttons {
    gap: var(--spacing-xs);
  }

  .auth-buttons .btn {
    padding: var(--spacing-xs) var(--spacing-sm);
    font-size: var(--font-size-xs);
  }
}

@media (max-width: 480px) {
  .nav-container {
    height: 60px;
    padding: 0 var(--spacing-sm);
  }

  .brand-text {
    font-size: var(--font-size-base);
  }

  .brand-icon {
    width: 32px;
    height: 32px;
  }
}
</style>
